class Solution
{
public:
    int maxProfitAssignment(vector<int> &difficulty, vector<int> &profit, vector<int> &worker)
    {
        int n = difficulty.size();
        vector<int> ids(n);
        iota(ids.begin(), ids.end(), 0);
        sort(ids.begin(), ids.end(), [&](auto i1, auto i2)
             { return difficulty[i1] < difficulty[i2]; });

        int maxProfit = 0;
        int result = 0;

        sort(worker.begin(), worker.end());
        int i = 0;

        for (int w : worker)
        {
            while (i < n && difficulty[ids[i]] <= w)
            {
                maxProfit = max(maxProfit, profit[ids[i]]);
                ++i;
            }
            result += maxProfit;
        }

        return result;
    }
};